package iec104;

public class REASON {
	public static final int COT_PERIODIC = 1;
	public static final int COT_BACKGROUND_SCAN = 2;
	public static final int COT_SPONTANEOUS = 3;
	public static final int COT_INITIALIZED = 4;
	public static final int COT_REQUEST = 5;
	public static final int COT_ACTIVATION = 6;
	public static final int COT_ACTIVATION_CON = 7;
	public static final int COT_DEACTIVATION = 8;
	public static final int COT_DEACTIVATION_CON = 9;
	public static final int COT_ACTIVATION_TERMINATION = 10;
	public static final int COT_RETURN_INFO_REMOTE = 11;
	public static final int COT_RETURN_INFO_LOCAL = 12;
	public static final int COT_FILE_TRANSFER = 13;
	public static final int COT_AUTHENTICATION = 14;
	public static final int COT_MAINTENANCE_OF_AUTH_SESSION_KEY = 15;
	public static final int COT_MAINTENANCE_OF_USER_ROLE_AND_UPDATE_KEY = 16;
	public static final int COT_INTERROGATED_BY_STATION = 20;
	public static final int COT_INTERROGATED_BY_GROUP_1 = 21;
	public static final int COT_INTERROGATED_BY_GROUP_2 = 22;
	public static final int COT_INTERROGATED_BY_GROUP_3 = 23;
	public static final int COT_INTERROGATED_BY_GROUP_4 = 24;
	public static final int COT_INTERROGATED_BY_GROUP_5 = 25;
	public static final int COT_INTERROGATED_BY_GROUP_6 = 26;
	public static final int COT_INTERROGATED_BY_GROUP_7 = 27;
	public static final int COT_INTERROGATED_BY_GROUP_8 = 28;
	public static final int COT_INTERROGATED_BY_GROUP_9 = 29;
	public static final int COT_INTERROGATED_BY_GROUP_10 = 30;
	public static final int COT_INTERROGATED_BY_GROUP_11 = 31;
	public static final int COT_INTERROGATED_BY_GROUP_12 = 32;
	public static final int COT_INTERROGATED_BY_GROUP_13 = 33;
	public static final int COT_INTERROGATED_BY_GROUP_14 = 34;
	public static final int COT_INTERROGATED_BY_GROUP_15 = 35;
	public static final int COT_INTERROGATED_BY_GROUP_16 = 36;
	public static final int COT_REQUESTED_BY_GENERAL_COUNTER = 37;
	public static final int COT_REQUESTED_BY_GROUP_1_COUNTER = 38;
	public static final int COT_REQUESTED_BY_GROUP_2_COUNTER = 39;
	public static final int COT_REQUESTED_BY_GROUP_3_COUNTER = 40;
	public static final int COT_REQUESTED_BY_GROUP_4_COUNTER = 41;
	public static final int COT_UNKNOWN_TYPE_ID = 44;
	public static final int COT_UNKNOWN_COT = 45;
	public static final int COT_UNKNOWN_CA = 46;
	public static final int COT_UNKNOWN_IOA = 47;

	private short m_srcAddr = 0;
	private short reason = 0;

	public short setReasonValue(short reasonvalue)
	{
		reason=(short) (reasonvalue&0xFF);
		m_srcAddr=(short) ((reasonvalue >> 8)&0xFF);
		return getReason();
	}
	
	public void setReason(int value, int m_pn, int m_t, int srcaddr) {
		this.reason |= (value & 0x3F);
		this.reason |= (m_pn << 6);
		this.reason |= (m_t << 7);
		m_srcAddr = (short) (srcaddr & 0xff);
	}

	public short getReason() {
		return (short) ((m_srcAddr << 8) | (reason & 0xFF));
	}

	public String tostring() {
		String reasonstr = new String("Reason:");
		int typeid=reason & 0x3F;
		switch (typeid) {
		case COT_PERIODIC:
			reasonstr.concat("PERIODIC");
			break;
		case COT_BACKGROUND_SCAN:
			reasonstr.concat("BACKGROUND_SCAN");
			break;
		case COT_SPONTANEOUS:
			reasonstr.concat("SPONTANEOUS");
			break;
		case COT_INITIALIZED:
			reasonstr.concat("INITIALIZED");
			break;
		case COT_REQUEST:
			reasonstr.concat("REQUEST");
			break;
		case COT_ACTIVATION:
			reasonstr.concat("ACTIVATION");
			break;
		case COT_ACTIVATION_CON:
			reasonstr.concat("ACTIVATION_CON");
			break;
		case COT_DEACTIVATION:
			reasonstr.concat("DEACTIVATION");
			break;
		case COT_DEACTIVATION_CON:
			reasonstr.concat("DEACTIVATION_CON");
			break;
		}
		return reasonstr;
	}
}
